package com.kingdee.shr.affair.web.handler.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class FileDocxUtil {

	
	/**
	 * 替换文档中的参数（word）
	 * @param doc
	 * @param params
	 */
	public static   void replaceText(XWPFDocument doc, Map<String, Object> params) {
	    Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
	    XWPFParagraph para;
	    while (iterator.hasNext()) {
	        para = iterator.next();
	        replaceInPara(para, params);
	    }
	}

	
	/**
	 * 替换段落中的参数（word）
	 * @param para
	 * @param params
	 */
	public static  void replaceInPara(XWPFParagraph para, Map<String, Object> params) {
	    List<XWPFRun> runs;
	    Matcher matcher;
	    if (matcher(para.getParagraphText()).find()) {
	        runs = para.getRuns();
	        for (int i=0; i<runs.size(); i++) {
	            XWPFRun run = runs.get(i);
	            String runText = run.toString();
	            matcher = matcher(runText);
	            if (matcher.find()) {
	                while ((matcher = matcher(runText)).find()) {
	                    runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));
	                }
	                para.removeRun(i);
	                //重新插入run里内容格式可能与原来模板的格式不一致
	                para.insertNewRun(i).setText(runText);
	            }
	        }
	    }
	}
	
	/**
	 * 处理文档替换内容的格式问题（word）
	 * @param tempDoc
	 * @param doc
	 */
	public  static void setStyle(XWPFDocument tempDoc,XWPFDocument doc) {
	    Iterator<XWPFParagraph> iterator = tempDoc.getParagraphsIterator();
	    Iterator<XWPFParagraph> iterator2 = doc.getParagraphsIterator();
	    XWPFParagraph para ;
	    XWPFParagraph para2;
	    while (iterator.hasNext()&&iterator2.hasNext()) {
	        para = iterator.next();
	        para2 = iterator2.next();
	        setStyleInPara(para,para2);
	    }
	}

	/**
	 * 处理段落替换内容的格式问题（word）
	 * @param para
	 * @param para2
	 */
	public  static void setStyleInPara(XWPFParagraph para, XWPFParagraph para2) {
	    List<XWPFRun> runs;
	    List<XWPFRun> runs2;
	    Matcher matcher;
	    if (matcher(para.getParagraphText()).find()) {
	        runs = para.getRuns();
	        runs2 = para2.getRuns();
	        for (int i=0; i<runs.size(); i++) {
	            XWPFRun run = runs.get(i);
	            XWPFRun run2 = runs2.get(i);
	            String runText = run.toString();
	            matcher = matcher(runText);
	            if (matcher.find()) {
	                //按模板文件格式设置格式
	                run2.getCTR().setRPr(run.getCTR().getRPr());
	            }
	        }
	    }
	}
	
	public static Matcher matcher(String str) {
		 Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
		 Matcher matcher = pattern.matcher(str);
		 return matcher;
	}
	
	
	
}
